=begin
  File Creation Date: July 10,2011
  File Name: libs_active_form.rb
  Date Last Modified: July 18,2011
  Modification: include application helper
  Original Author: Bryan Salazar
  Description: To have model that don't need to connect to database and 
    validate specific attribute
=end
class LibsActiveForm < ActiveRecord::Base
  # include application helper so that it can access in model
  include ApplicationHelper

  def self.columns() @columns ||= []; end

  def self.column(name,sql_type=nil,default=nil,null=true)
    columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s,default,sql_type.to_s,null)
  end

  def save(validate=true)
    validate? valid? :true
  end

#  def self.valid_attribute?(attrib, value)
#    mock = self.new(attrib => value)
#    unless mock.valid?
#      return !mock.errors.on(attrib).present?
#    end
#    true
#  end
  
  def valid_attribute?(options={},*attrs)
    self.valid?
    
    defaults = {
      :except => ''
    }
    defaults.merge!(options)
    
    if defaults[:except].to_s != ''
      defaults[:except].each do |v|
        self.errors.instance_variable_get('@errors').delete(v)
      end
    else  
      self.class.column_names.each do |v|
        if !attrs.include?(v)
          self.errors.instance_variable_get('@errors').delete(v)
        end
      end
    end 
    
#    abort(self.errors.count.to_s)
    if self.errors.count.to_i >= 1
      return false
    end
    return true
    
  end
  
end
